package com.kungfuhacking.wristbandpro.find.presenter;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.find.bean.PayBean;
import com.kungfuhacking.wristbandpro.find.view.ISubView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;

import io.reactivex.disposables.Disposable;

/**
 * Created by Aaron on 17/7/17.
 */

public class ISubPresenter implements BasePresenter {

    private ISubView iSubView;

    public ISubPresenter(ISubView iSubView) {
        this.iSubView = iSubView;
    }

    public void sub(String phoneNum) {

        JSONObject j = new JSONObject();
        j.put("token", iSubView.getToken());
        j.put("phoneNum", phoneNum);

        RetrofitHttpUtil.getInstance().identityPhoneNum(new BaseNoTObserver<PayBean>() {
            @Override
            public void onHandleSuccess(PayBean basebean) {
                iSubView.hideLoading();

                if (basebean != null && !TextUtils.isEmpty(basebean.getUrl())) {
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(basebean.getUrl());
                    intent.setData(content_url);
                    iSubView.nextActivityWithIntent(intent);
                } else {
                    iSubView.showErr("数据错误");
                }

            }

            @Override
            public void onHandleError(String message) {
                iSubView.hideLoading();
                iSubView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iSubView.hideLoading();
                iSubView.showErr(message);
                iSubView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {

                iSubView.showLoading("请求中...");

            }
        }, new RequestBean().setData(j.toString()));

    }


    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
